package com.zjsru.oneDay202205;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: likew
 * @Date: 2022/6/4 独特的电子邮件地址 输入：emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
 * 输出：2 解释：实际收到邮件的是 "testemail@leetcode.com" 和 "testemail@lee.tcode.com"。
 * <p>
 * 输入：emails = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"] 输出：3
 */
public class numUniqueEmails {
    
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();
        for (String s : emails) {
            StringBuffer sb = new StringBuffer();
            int len = s.length(), i = 0;
            boolean b = false;
            while (i < len) {
                char c = s.charAt(i);
                if (c == '.' && ++i >= 0) {
                    continue;
                }
                if (c == '+') {
                    b = false;
                }
                if (c == '@') {
                    break;
                }
                if (b) {
                    sb.append(c);
                }
                i++;
            }
            set.add(sb.append(s.substring(i)).toString());
        }
        return set.size();
    }
    
    public static void main(String[] args) {
        numUniqueEmails numUniqueEmails = new numUniqueEmails();
        String[] emails = new String[] {"test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com",
                "testemail+david@lee.tcode.com"};
        System.out.println(numUniqueEmails.numUniqueEmails(emails));
    }
}
